package br.com.fiap.trabalho.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class JPAAuxiliarDAO {

	private EntityManager em;
	
	private void createEntityManager() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistenceUnit"); 
		em = emf.createEntityManager(); 
	}

	public int selecionaMaxCategory() {
		createEntityManager();

		int valor = 0;
		
		try {			
			Query query = em.createQuery("SELECT MAX(c.id) FROM Category c ");
			valor = (Integer) query.getSingleResult();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valor;
	}
	
	public int selecionaMaxStudio() {
		createEntityManager();

		int valor = 0;
		
		try {			
			Query query = em.createQuery("SELECT MAX(s.id) FROM Studio s ");
			valor = (Integer) query.getSingleResult();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valor;
	}
	
	public int selecionaMaxActor() {
		createEntityManager();

		int valor = 0;
		
		try {			
			Query query = em.createQuery("SELECT MAX(a.id) FROM Actor a ");
			valor = (Integer) query.getSingleResult();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valor;
	}
	
	public int selecionaMaxMovie() {
		createEntityManager();

		int valor = 0;
		
		try {			
			Query query = em.createQuery("SELECT MAX(m.id) FROM Movie m ");
			valor = (Integer) query.getSingleResult();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return valor;
	}
}
